import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import store from '@/store'

Vue.use(VueRouter)
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

const map = {
  '/home/shouye':{path: '/home/shouye',name: 'shouye',component: () => import('@/components/shouye.vue')},
  '/home/shop':{path: '/home/shop',name: 'shop',component: () => import('@/components/shop.vue')},
  '/home/user':{path: '/home/user',name: 'user',component: () => import('@/components/user.vue')}, 
  '/home/staff':{path:  '/home/staff',name: 'staff',component: () => import('@/components/staff.vue')},
  '/home/addshop': {path: '/home/addshop',name: 'addshop',component: () => import('@/components/addshop.vue')},
  '/home/adduser':{path: '/home/adduser',name: 'adduser',component: () => import('@/components/adduser.vue')},
  '/home/addstaff':{path: '/home/addstaff',name: 'addstaff',component: () => import('@/components/addstaff.vue')},
  '/home/updateshop':{path: '/home/updateshop',name: 'updateshop',component: () => import('@/components/updateshop.vue')},
  '/home/updateuser':{path: '/home/updateuser',name: 'updateuser',component: () => import('@/components/updateuser.vue')}
}
// {path: '/home/shouye',name: 'shouye',component: () => import('@/components/shouye.vue')},
// {path: '/home/shop',name: 'shop',component: () => import('@/components/shop.vue')},
// {path: '/home/user',name: 'user',component: () => import('@/components/user.vue')}, 
// {path: '/home/addshop',name: 'addshop',component: () => import('@/components/addshop.vue')},
// {path: '/home/staff',name: 'staff',component: () => import('@/components/staff.vue')},
// {path: '/home/adduser',name: 'adduser',component: () => import('@/components/adduser.vue')},
// {path: '/home/addstaff',name: 'addstaff',component: () => import('@/components/addstaff.vue')}
const routes = [{
    path: '/',
    name: 'Home',
    component: Home,
    redirect:'/home/shouye',
    children: [
      
    ],
  },
  {
    path: '/about',
    name: 'About',
     component: () => import('@/views/Login.vue')
  },
  {
    path: "/login",
    name: "name",
    component: () => import('@/views/Login.vue')
  },
  {
    path: "/register",
    name: "name",
    component: () => import('@/views/Register.vue')
  }
]


const router = new VueRouter({
  routes
})

// 动态路由核心技术
export const initDynamicRouter = () => {
  const routes = router.options.routes
  const route = routes.find(route => route.name === 'Home')
  const rights = store.state.user.rights || sessionStorage.getItem('v-user') || []
  rights.forEach(right => {
    right.children.forEach(child => {
      if (map[child.path]) {
        route.children.push(map[child.path])
      }
    })
  })
  router.addRoutes(routes)
}
initDynamicRouter()
router.beforeEach((to,from,next)=>{
  if(to.path === '/login' || to.path == '/register') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if(!tokenStr) return next('/login')
  next()
})
export default router